<html lang="en">
	<head>
		<title>Yuka | Perception | Line of Sight</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="../../lib/styles.css">
		<link rel="shortcut icon" type="image/x-icon" href="https://mugen87.github.io/yuka/favicon.ico">
	</head>
<body>

	<section id="info">
		<p>
			The white fan represents the visibility range of the game entity.<br/>
			When the target is visible for the game entity, the target's color changes to green.
		</p>
	</section>

	<script type="module">

		import * as YUKA from '../../../build/yuka.module.js';
		import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.109/build/three.module.js';

		import { createVisionHelper } from '../common/VisionHelper.js';
		import { Obstacle } from '../common/Obstacle.js';

		let renderer, scene, camera, targetMaterial;

		let entityManager, time, entity, target;

		init();
		animate();

		function init() {

			scene = new THREE.Scene();

			camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 1000 );
			camera.position.set( 0, 10, 10 );
			camera.lookAt( scene.position );

			//

			const entityGeometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
			entityGeometry.rotateX( Math.PI * 0.5 );
			const entityMaterial = new THREE.MeshNormalMaterial();

			const entityMesh = new THREE.Mesh( entityGeometry, entityMaterial );
			entityMesh.matrixAutoUpdate = false;
			scene.add( entityMesh );

			const obstacleGeometry = new THREE.PlaneBufferGeometry( 2, 2, 5, 5 );
			obstacleGeometry.rotateY( Math.PI );
			const obstacleMaterial = new THREE.MeshBasicMaterial( { color: 0x777777, side: THREE.DoubleSide } );

			const obstacleMesh = new THREE.Mesh( obstacleGeometry, obstacleMaterial );
			obstacleMesh.matrixAutoUpdate = false;
			scene.add( obstacleMesh );

			const targetGeometry = new THREE.SphereBufferGeometry( 0.05 );
			targetMaterial = new THREE.MeshBasicMaterial();

			const targetMesh = new THREE.Mesh( targetGeometry, targetMaterial );
			targetMesh.matrixAutoUpdate = false;
			scene.add( targetMesh );

			//

			const grid = new THREE.GridHelper( 10, 25 );
			scene.add( grid );

			//

			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			//

			window.addEventListener( 'resize', onWindowResize, false );

			// game setup

			entityManager = new YUKA.EntityManager();
			time = new YUKA.Time();

			const vertices = obstacleGeometry.attributes.position.array;
			const indices = obstacleGeometry.index.array;
			const geometry = new YUKA.MeshGeometry( vertices, indices );

			const obstacle = new Obstacle( geometry );
			obstacle.position.z = 3;
			obstacle.setRenderComponent( obstacleMesh, sync );

			target = new YUKA.GameEntity();
			target.setRenderComponent( targetMesh, sync );

			entity = new YUKA.GameEntity();
			entity.setRenderComponent( entityMesh, sync );

			const vision = new YUKA.Vision( entity );
			vision.range = 5;
			vision.fieldOfView = Math.PI * 0.5;
			vision.addObstacle( obstacle );
			entity.vision = vision;

			const helper = createVisionHelper( vision );
			entityMesh.add( helper );

			entityManager.add( entity );
			entityManager.add( obstacle );
			entityManager.add( target );

		}

		function onWindowResize() {

			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();

			renderer.setSize( window.innerWidth, window.innerHeight );

		}

		function animate() {

			requestAnimationFrame( animate );

			const delta = time.update().getDelta();
			const elapsed = time.getElapsed();

			// change color of target if visible

			target.position.set( Math.sin( elapsed * 0.5 ) * 4, 0, 4 );

			if ( entity.vision.visible( target.position ) === true ) {

				targetMaterial.color.set( 0x00ff00 );

			} else {

				targetMaterial.color.set( 0xff0000 );

			}

			//

			entityManager.update( delta );

			renderer.render( scene, camera );

		}

		function sync( entity, renderComponent ) {

			renderComponent.matrix.copy( entity.worldMatrix );

		}

	</script>

</body>
</html>
